/*
	All calls of OutputLog() are just for an output for debuging purposes.
	They are just to make see if something went wrong, and has mostly only
	told me when somene banned has tried to log in.

	Files are defined in the .dme file so they can be called anything.
*/
proc
	ReturnKeyID(var/source as text)
		var/count = 1
		var/savefile/Players = new(KeyList)
		Players.cd = "/"; Players.eof = -1
		source = ckey(source)

		while(count != Players["Total"]+1)
			if(source in Players["[count]"])
				return count
			count++
		return 0

	ReturnComputerID(var/computer as text)
		var/count = 1
		var/savefile/CID = new(CIDList)
		CID.cd = "/"; CID.eof = -1
		while(count != CID["Total"]+1)
			if(computer in CID["[count]"])
				return count
			count++
		return 0

	ReturnAddressID(var/address as text)
		var/count = 1
		var/savefile/IPS = new(IPList)
		IPS.cd = "/"; IPS.eof = -1
		while(count != IPS["Total"]+1)
			if(address in IPS["[count]"])
				return count
			count++
		return 0

	ReturnKeys(target)
		var/ID = ReturnKeyID(target)
		var/savefile/Keys = new(KeyList)
		var/output = ""

		for(var/K in Keys["[ID]"]) output += "\"[K]\" "

		return output

	UpdateValue(var/ID as num, var/value as text, var/type as text)

		var/savefile/Person = new("[People][ID].sav")
		var/list/NewList = list()
		Person["[ID]"] = list()

		for(var/V in Person["[ID]/[type]s"]) if(V == value) return 0

		NewList = Person["[ID]/[type]s"]
		NewList.Add(value)
		Person["[ID]/[type]s"] = NewList

		if(type == "Key")
			var/savefile/Players = new(KeyList)
			Players["[ID]"] = Person["[ID]/[type]s"]

		else if(type == "IP")
			var/savefile/Players = new(IPList)
			Players["[ID]"] = Person["[ID]/[type]s"]
		else if(type == "CID")
			var/savefile/Players = new(CIDList)
			Players["[ID]"] = Person["[ID]/[type]s"]
		else
			return 0

		OutputLog(BLUE,"Adding [type] of [value] to [ID]")


	checkMute(target)
		var/ID = ReturnKeyID(target)
		var/savefile/Chart = new("[People][ID].sav")
		Chart.cd = "/"; Chart.eof=-1
		target = ckey(target)

		if(target in MuteList) return 1
		if(ID == 0) return 0
		else return Chart["[ID]/Muted"]

	checkBan(target)
		var/ID = ReturnKeyID(target)
		var/savefile/Chart = new("[People][ID].sav")
		Chart.cd = "/"; Chart.eof=-1
		target = ckey(target)

		if(target in Banned) return 1
		if(ID == 0) return 0
		else return Chart["[ID]/Banned"]

client/proc


	// Add an initial archive for a user (Do not call this)
	NewUser()
		set hidden = 1

		var/savefile
			Keys = new(KeyList)
			IPS = new(IPList)
			CID = new(CIDList)

		if(Keys["Total"] <= 1)
			Keys["Total"] = Keys["Total"] + 1
			IPS["Total"] = IPS["Total"] + 1
			CID["Total"] = CID["Total"] + 1

		var/ID = Keys["Total"]

		var/list/Key = list()
		Key.Add(src.ckey)

		var/list/IP = list()
		IP.Add(src.address)

		var/list/computer = list()
		computer.Add(computer_id)

		Keys["[ID]"] = list()
		IPS["[ID]"] = list()
		CID["[ID]"] = list()

		Keys["[ID]"] = Key
		IPS["[ID]"] = IP
		CID["[ID]"] = computer

		var/savefile/Archive = new("[People][ID].sav")
		Archive["[ID]/Keys"] = Key
		Archive["[ID]/IPs"] = IP
		Archive["[ID]/CIDs"] = computer

		Archive["[ID]/Joined"] = time2text(world.timeofday,"Day Month DD, YYYY. (hh:mm:ss EST)")
		Archive["[ID]/Muted"] = 0
		Archive["[ID]/Banned"] = 0

		Archive["[ID]/Notes"] = "There are currently no notes on this person."
		Archive["[ID]/Punishments"] = 0

		OutputLog(BLUE,"[src.key] has been archived as [ID].")
		Keys["Total"] = ID
		IPS["Total"] = ID
		CID["Total"] = ID



	// Always call this to create or update an entry
	// Call this on login so that information for every person is always up to date
	UpdateArchive()
		// Check for an ID first
		var/ID = 0
		var/savefile
			Keys = new(KeyList)
			IPS = new(IPList)
			CID = new(CIDList)

		/// Check if the archive exists, if not skip everything and make a new user.
		if(!Keys["Total"])
			Keys["Total"] = 0
			IPS["Total"] = 0
			CID["Total"] = 0
			src.NewUser()

		else

			// Check if they exist
			ID = ReturnKeyID(src.ckey)
			OutputLog(NORMAL,"Searching for [src.key]'s ID -- Found [ID]")

			if(!ID)
				if(!IsTelnet())
					ID = ReturnComputerID(src.computer_id)
					OutputLog(NORMAL,"Searching for [src.key]'s CID -- ID is now [ID]")
			if(!ID)
				ID = ReturnAddressID(src.address)
				OutputLog(NORMAL,"Searching for [src.key]'s IP -- ID is now [ID]")
			if(!ID)	src.NewUser()

			else
				if(!(src.ckey in Keys["[ID]"])) UpdateValue(ID, src.ckey, "Key")
				if(!(src.address in IPS["[ID]"])) UpdateValue(ID, src.address, "IP")
				if(!IsTelnet()) if(!(src.computer_id in CID["[ID]"])) UpdateValue(ID, src.computer_id, "CID")


	// ADMIN
	AddPunishment(Admin, Player, Type)
		set hidden = 1
		set category = "Admin"

		var/ID = ReturnKeyID(Player)

		var/savefile/Person = new("[People][ID].sav")


		if(ID == 0)
			OutputLog(RED,"ID for [Player] is 0, falling back to internal system to [Type].")
			if(Type == "Mute")
				MuteList+=Player
			else if(Type == "Ban")
				Banned+=Player
			else if(Type == "Unmute")
				MuteList-=Player
			else if(Type == "Unban")
				Banned-=Player
			else if(Type == "Kick")
				var/client/person = findTarget(Player)
				del(person)
		else

			///////////////

			if(Type == "Mute")
				Person["[ID]/Muted"] = 1
				MuteList+=Player
				Person["[ID]/Punishments"] = Person["[ID]/Punishments"] + 1

			else if(Type == "Unmute")
				Person["[ID]/Muted"] = 0
				MuteList-=Player

			else if(Type == "Ban")
				Person["[ID]/Banned"] = 1
				Banned+=Player
				for(var/client/C in Everyone)
					if(C.key in Person["[ID]/Keys"])
						var/client/person = findTarget(C.key)
						person.Kicked = TRUE
						del(person)
				Person["[ID]/Punishments"] = Person["[ID]/Punishments"] + 1

			else if(Type == "Unban")
				Person["[ID]/Banned"] = 0
				Banned-=Player

			else if(Type == "Kick")
				for(var/client/C in Everyone)
					if(C.key in Person["[ID]/Keys"])
						var/client/person = findTarget(C.key)
						person.Kicked = TRUE
						del(person)
				Person["[ID]/Punishments"] = Person["[ID]/Punishments"] + 1

			OutputLog(RED,"ID of [ID] has been issued \"[Type]\".")
			///////////////

			if(Type != "Kick") ViewID(ID)

client/verb
	FindID()
		set hidden = 1
		if(!(src.ckey in Admins)) return
		var/savefile/Keys = new(KeyList)
		var/txtfile = file(PTEXT)

		fdel(txtfile)
		Keys.ExportText("/",txtfile)

		src<<browse(txtfile,"window=1")

	adminSaveNotesButton()
		set hidden = 1
		if(!(src.ckey in Admins)) return
		var/ID = winget(src,"Admin_ArchiveEntry.ID","text")
		var/Notes = winget(src,"Admin_ArchiveEntry.Notes","text")
		var/savefile/Chart = new("server/Archives/Chart.sav")
		Chart["[ID]/Notes"] = Notes

	ViewID(var/id as num|null)
		set hidden = 1
		if(!(src.ckey in Admins)) return

		var/ID = 0
		if(id) ID = id
		else ID = winget(src,"Admin_Settings.idinput","text")

		var/savefile/Person = new("[People][ID].sav")

		var
			Keys = list()
			IPs = list()
			CIDs = list()

		var
			Joined = Person["[ID]/Joined"]
			Muted = Person["[ID]/Muted"]
			Banned = Person["[ID]/Banned"]
			Punishments = Person["[ID]/Punishments"]
			Notes = Person["[ID]/Notes"]

		Keys = Person["[ID]/Keys"]
		IPs = Person["[ID]/IPs"]
		CIDs = Person["[ID]/CIDs"]

		if(Muted == 1) Muted = "Yes"
		else Muted = "No"

		if(Banned == 1) Banned = "Yes"
		else Banned = "No"

		winset(src,"Admin_ArchiveEntry.ID","text=\"[ID]\"")
		winset(src,"Admin_ArchiveEntry.Muted","text=\"[Muted]\"")
		winset(src,"Admin_ArchiveEntry.Joined","text=\"  [Joined]\"")
		winset(src,"Admin_ArchiveEntry.Banned","text=\"[Banned]\"")
		winset(src,"Admin_ArchiveEntry.Punishments","text=\"[Punishments]\"")

		src<<output(null,"Admin_ArchiveEntry.Keys")
		src<<output(null,"Admin_ArchiveEntry.IPs")
		src<<output(null,"Admin_ArchiveEntry.CIDs")

		for(var/K in Keys) src<<output(K,"Admin_ArchiveEntry.Keys")
		for(var/I in IPs) if(I != null) src<<output(I,"Admin_ArchiveEntry.IPs")
		for(var/C in CIDs) if(C != null) src<<output(C,"Admin_ArchiveEntry.CIDs")

		winset(src,"Admin_ArchiveEntry.Notes","text=\"[Notes]\"")

		winset(src,"Admin_ArchiveEntry","size=495x575")
		winset(src,"Admin_ArchiveEntry","is-visible=true")